<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-col>
          <el-button type='primary'>添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="roleslist" border stripe>
        <el-table-column type='expand'>
          <template slot-scope="scope">
            <el-row v-for='(item,i) in scope.row.children' :key='item.id'
              :class="['bdbottom',i===0?'bdtop':'','vcenter']">
              <!-- 一级权限 -->
              <el-col :span='5'>
                <el-tag>{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span='19'>
                <!-- 二级权限 -->
                <el-row v-for="(item2,i2) in item.children" :key='item2.id' :class="[i2===0?'':'bdtop','vcenter']">
                  <el-col :span='6'>
                    <el-tag type='success'>{{item2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span='18'>
                    <!-- 三级权限 -->
                    <el-tag @close='removeRightById(item3.id,scope.row)' closable type='warning'
                      v-for="item3 in item2.children " :key='item3.id'>{{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label='操作' width='300px'>
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon='el-icon-edit'>编辑</el-button>
            <el-button type="danger" size="mini" icon='el-icon-delete'>删除
            </el-button>
            <el-tooltip effect='dark' content='分配权限' placement='top' :enterable='false'>
              <el-button type="warning" size="mini" icon='el-icon-setting' @click='showRightDialog(scope.row)'>分配权限
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <TableList :col='row'></TableList>
      </el-table>

      <el-dialog title="权限分配" :visible.sync="showRightsDialogVisble" width="50%" @close='setRightDialogClose'>
        <el-tree :data="rightslist" show-checkbox node-key="id" default-expand-all
          :default-checked-keys="defKeys" :props="defaultProps" ref='treeRef'>
        </el-tree>
        <span slot="footer">
          <el-button @click="showRightsDialogVisble = false" class="quxiao">关闭</el-button>
          <el-button type="primary" @click="editRightsDialog">确定</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script scoped>
import { rloesListAPI, RightsDeleteAPI, rightsEditAPI, rigthsAPI } from '@/api/roles'
import TableList from '@/components/table.vue'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: { TableList },
  // 组件状态值
  data () {
    return {
      // 权限分配对话框显示和隐藏
      showRightsDialogVisble: false,
      // 树形控件绑定对象
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      rolesId: '',
      defKeys: [],
      // 所有权限数据
      rightslist: [],
      // 角色列表数据
      roleslist: [],
      row: [
        {
          name: '角色名称',
          type: 'roleDesc'
        },
        {
          name: '角色描述',
          type: 'roleName'
        }
      ]
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    setRightDialogClose () {
      this.defKeys = []
    },
    // 分配权限
    async showRightDialog (roles) {
      const { data: res } = await rightsEditAPI()
      if (res.meta.status !== 200) return this.$message.error('获取权限数据失败！')
      this.rightslist = res.data
      this.rolesId = roles.id
      this.getLeafKeys(roles)
      this.showRightsDialogVisble = true
    },
    async editRightsDialog () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const dtStr = keys.join(',')
      const { data: res } = await rigthsAPI(this.rolesId, { rids: dtStr })
      if (res.meta.status !== 200) return this.$message.error('更新失败')
      this.$message({
        message: '更新成功',
        type: 'success'
      })
      this.getRolesList()
      this.showRightsDialogVisble = false
    },
    // 标签删除
    async removeRightById (id, row) {
      const confirmResult = await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('取消删除')
      const { data: res } = await RightsDeleteAPI(id, row.id)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      row.children = res.data
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      // this.getRolesList()
    },
    // 渲染数据
    async getRolesList () {
      const { data: res } = await rloesListAPI()
      if (res.meta.status !== 200) return this.$message.error('获取权限列表失败')
      this.roleslist = res.data
      this.$message.success('获取权限列表成功')
    },
    getLeafKeys (node) {
      if (!node.children) return this.defKeys.push(node.id)
      node.children.forEach(item => this.getLeafKeys(item))
    }
  },
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.getRolesList()
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  }
}
</script>

<style scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
.el-table {
  margin-top: 20px;
}
</style>
